/*
// * Copyright 2009 Jinbo Lin. All rights reserved.
 * 
 * cn.edu.pku.dao.impl HumanResourcesManagementSystem
 *
 * Created on 2009-12-20
 */
package cn.edu.pku.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.edu.pku.dao.UsermgtDao;
import cn.edu.pku.model.Usermgt;

/**
 * @author Jinbo Lin
 * 
 *  
 */
public class UsermgtDaoHibernate extends HibernateDaoSupport implements
		UsermgtDao {

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.UsermgtDao#delete(int)
	 */
	@Override
	public void delete(int id) {
		if (null != this.get(id)) {
			this.getHibernateTemplate().delete(
				this.getHibernateTemplate().get(Usermgt.class, id));
		}

	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.UsermgtDao#delete(cn.edu.pku.model.Usermgt)
	 */
	@Override
	public void delete(Usermgt usermgt) {
		if (null != this.get(usermgt.getUserId())) {
			this.getHibernateTemplate().delete(usermgt);
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.UsermgtDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Usermgt> findAll() {
		String hql = "from Usermgt";
		return this.getHibernateTemplate().find(hql);
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.UsermgtDao#get(int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Usermgt get(int id) {
		String hql = "from Usermgt usermgt where usermgt.userId=?";
		List<Usermgt> usermgts = this.getHibernateTemplate().find(
				hql, new Integer(id));
		Usermgt usermgt = (null != usermgts && 0 != usermgts.size()) ? usermgts.get(0) : null;
		return usermgt;
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.UsermgtDao#save(cn.edu.pku.model.Usermgt)
	 */
	@Override
	public Serializable save(Usermgt usermgt) {
		if(null == this.get(usermgt.getUserId())) {
			this.getHibernateTemplate().save(usermgt);
		} else {
			usermgt = null;
		}
		return usermgt;
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.UsermgtDao#update(cn.edu.pku.model.Usermgt)
	 */
	@Override
	public void update(Usermgt usermgt) {
		if (null != this.get(usermgt.getUserId())) {
//			this.getHibernateTemplate().saveOrUpdate(user);
			this.getHibernateTemplate().update(usermgt);
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.UsermgtDao#tDelete(int)
	 */
	@Override
	public void tDelete(int id) {
		Usermgt usermgt = this.get(id);
		usermgt.setRemark("Removed");
		this.save(usermgt);
	}

//	public static void main(String[] args) {
//		Resource resource = new ClassPathResource("applicationContext.xml");
//		BeanFactory factory = new XmlBeanFactory(resource);
//		HibernateTemplate hibernateTemplate = (HibernateTemplate) factory
//				.getBean("hibernateTemplate");
//		UsermgtDaoHibernate udh = new UsermgtDaoHibernate();
//		udh.setHibernateTemplate(hibernateTemplate);
//
//		Usermgt usermgt = null;
//
//		/*usermgt = new Usermgt(1, 1, 2, "yes", new Date());
//		udh.save(usermgt);*/
//		
//		/*usermgt = udh.get(1);
//		System.out.println(usermgt.getCreatorId());
//		System.out.println(usermgt.getAuditorId());
//		System.out.println(usermgt.getAudit());
//		System.out.println(usermgt.getDateTime());*/
//
//		/*usermgt = udh.get(1);
//		usermgt.setUserId(10);
//		usermgt.setAuditorId(3);
//		udh.update(usermgt);*/
//
//		/*List<Usermgt> usermgts = udh.findAll();
//		for (Usermgt t : usermgts) {
//			System.out.println(t.getAuditorId());
//		}*/
//		
//		/*udh.delete(2);*/
//		
//		/*usermgt = new Usermgt(2, 1, 2, "yes", new Date());
//		udh.delete(usermgt);*/
//	}
}
